package com.traffic.between.service;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.traffic.between.config.BetweenProperties;
import com.traffic.between.util.Xml2JsonUtil;


public class YouNakangServiceImpl {
	private static Logger logger = Logger.getLogger(YouNakangServiceImpl.class);
	public static YouNakangServiceImpl service = new YouNakangServiceImpl();
	
	private static String HOST_ORDER_PATH = BetweenProperties.YOUNAKANG_ORDER_URL;
	private static String YOUNAKANG_APPKEY = BetweenProperties.YOUNAKANG_APPKEY;
	private static String YOUNAKANG_USERID = BetweenProperties.YOUNAKANG_USERID;
	private static String HOST_ORDERSTATUS_PATH = BetweenProperties.YOUNAKANG_ORDERSTATUS_URL;
	
	

	public String order(String productid, String mobile, String sporderid, String price,String back_url) {
		Date date=new Date();
		DateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
		String time=format.format(date); 
		String userid = YOUNAKANG_USERID;// 商户数字id
	
		String num = "1";// 固定值
	
		String spordertime = time;
		
		String key = YOUNAKANG_APPKEY;// 商户Key
		
 
        String sign = getMD5("userid="+userid+"&productid="+productid+"&price="+price+"&num="+num+"&mobile="+mobile+"&spordertime="+spordertime+"&sporderid="+sporderid+"&key="+key);
        String result = "";
        
       
	
        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
        pairList.add(new BasicNameValuePair("userid", userid));
        pairList.add(new BasicNameValuePair("productid", productid));
        pairList.add(new BasicNameValuePair("price", price));
        pairList.add(new BasicNameValuePair("num", num));
        pairList.add(new BasicNameValuePair("mobile", mobile));
        pairList.add(new BasicNameValuePair("spordertime", spordertime));
        pairList.add(new BasicNameValuePair("sporderid", sporderid));
        pairList.add(new BasicNameValuePair("sign", sign));
        pairList.add(new BasicNameValuePair("back_url", back_url));
       
		HttpPost httpPost = new HttpPost(HOST_ORDER_PATH);
		CloseableHttpClient client = HttpClients.createDefault();
		logger.info("between- 流量订购请求时的参数: " + pairList);
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			logger.info("between- 流量订购请求时的响应statusCode: " + statusCode);
			
			if (200 == statusCode) {
				HttpEntity entity = response.getEntity();
				result = Xml2JsonUtil.xmlToJson(EntityUtils.toString(entity, "utf-8")) ;
				logger.info("between- 尤纳康订购请求时的响应数据是: " + result);
			}
			
		} catch (Exception e) {
			
			e.printStackTrace();
	
	
		}

		return result;
	}

	
	
	
	
	
	
	public String orderStatus(String cstmOrderNo) {

		logger.info("between- 尤纳康流量查詢状态请求参数: params cstmOrderNo = " + cstmOrderNo);
		String userid = YOUNAKANG_USERID;
	  
	    String result = "";
	    List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
        pairList.add(new BasicNameValuePair("userid", userid));
        pairList.add(new BasicNameValuePair("sporderid", cstmOrderNo));
       
        
    	HttpPost httpPost = new HttpPost(HOST_ORDERSTATUS_PATH);
		CloseableHttpClient client = HttpClients.createDefault();
		
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
			HttpResponse response = client.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			
			if (200 == statusCode) {
				HttpEntity entity = response.getEntity();
				result = Xml2JsonUtil.xmlToJson(EntityUtils.toString(entity, "utf-8"));
				logger.info("between- 尤纳康流量查詢状态请求时的响应数据是: " + result);
			}
		} catch (Exception e) {
			logger.info("between- 尤纳康流量查詢状态请求时的响应数据是: " + result);
		}
		return result;
	}

	/**
	 * md5加密 ，用于生成sign
	 * 
	 * @param message
	 * @return
	 */
	public static String getMD5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			return new BigInteger(1, md.digest()).toString(16);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}




}
